# 从实例的角度去研究反射


# class A:
#
#     static_field = '静态属性'
#
#     def __init__(self, name, age):
#         self.name = name
#         self.age = age
#
#     def func(self):
#         print('in A func')
#
#
# obj = A('MC骚Q', 18)
# print(obj.name)
# #
# print(hasattr(obj,'name'))  # ***
# print(getattr(obj,'name1',None))  ***
# setattr(obj,'hobby', '玩') *
# print(getattr(obj,'hobby'))
# print(obj.hobby)
# delattr(obj,'name') *
# print(hasattr(obj,'name'))
# if hasattr(obj, 'static_field'):
#     print(getattr(obj,'static_field'))
#
# if hasattr(obj, 'func'):
#     # print(getattr(obj,'func'))
#     getattr(obj,'func')()


# 从类的角度研究反射
#
# class A:
#
#     static_field = '静态属性'
#
#     def __init__(self, name, age):
#         self.name = name
#         self.age = age
#
#     def func(self):
#         print(self)
#         print(self.name)
#         print('in A func')
#
# print(hasattr(A,'static_field'))
# print(getattr(A,'static_field'))

# obj = A('大表哥', 18)
# # print(getattr(A, 'func'))
# getattr(A,'func')(obj)


# 从当前脚本研究反射
# def func1():
#     print('in func1')
#
# def func2():
#     print('in func2')

# def func3():
#     print('in func3')

# l1 = [func1, func2, func3]
# l1 = [f'func{i}' for i in range(1,4)]

# class B:
#     static = 'B类'



import sys
# print(sys.modules)
this_modules = sys.modules[__name__]  # 获取当前脚本这个对象
# print(this_modules)
# print(getattr(this_modules,'func1'))
# getattr(this_modules,'func1')()
# cls = getattr(this_modules,'B')
# obj = cls()
# print(obj.static)
# l1 = [f'func{i}' for i in range(1,4)]
# for i in l1:
#     getattr(this_modules, i)()

#
# def func1():
#     print('in func1')
#
# func1()
#
# def func2():
#     print('in func2')

# 在其他模块研究反射
# import other_modules as om
# # om.test()
# getattr(om, 'test')()


# class Auth:
#
#     function_list = [('login','请登录'), ('register','请注册'), ('exit', '退出')]
#
#     def login(self):
#         print('登录函数')
#
#
#     def register(self):
#         print('注册函数')
#
#
#     def exit(self):
#         print('退出...')
#
#
# while 1:
#     obj = Auth()
#     for num,option in enumerate(obj.function_list,1):
#         print(num,option[1])
#     choice_num = input('请输入选择:').strip()
#     if hasattr(obj,func_name):
#         getattr(obj,func_name)()





